package cn.imvc.app.models;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.IndexData;
import cn.imvc.app.repositories.IndexDataRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class IndexDataModelImpl implements IndexDataModel {

	@Autowired
	IndexDataRepository indexDataRepository;
	
	public Page<IndexData> find(ViewRequest viewRequest) {
		return indexDataRepository.findAll("i","IndexData i",ModelUtil.OrderByIdDesc(viewRequest));
	}

	public IndexData get(int id) {
		return indexDataRepository.findOne(id);
	}

	public IndexData save(IndexData indexData) {
		return indexDataRepository.saveAndFlush(indexData);
	}

	public IndexData update(int id,IndexData indexData) {
		IndexData loadIndexData = indexDataRepository.getOne(id);
		BeanUtils.copyProperties(indexData, loadIndexData, new String[]{"id"});
		return indexDataRepository.saveAndFlush(loadIndexData);
	}

	public void delete(int id) {
		indexDataRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}

	public IndexData merge(IndexData indexData) {
		return indexDataRepository.saveAndFlush(indexData);
	}

}
